<?php

namespace App\Console\Commands;

use App\Models\SeckillProduct;
use Illuminate\Console\Command;
use App\Services\RewardGoldPrize\RewardGoldPrize;
use App\Services\CountGoldChildren\CountGoldChildren;
use App\Services\RefreshGoldProductTakeState\RefreshGoldProductTakeState;

class Init extends Command
{
    protected $signature = 'cc:init';

    protected $description = "Init everyday data from zero";

    public function handle()
    {
	    /**
	     * ========== Normal Tasks =====================================================================================
	     */



	    /**
	     * ========== Gold Storm Tasks =================================================================================
	     */

	     $products = SeckillProduct::query()->with(['product' => function($query){
		    	$query->join('product_skus', 'product_skus.product_id','products.id')
		    		->select("product_skus.price_vip","product_skus.stock","products.id")
		    		->where('product_skus.stock','>',0);
		    }])
		 ->get();

		 $data = [];
		 if($products){
		 	foreach($products as $k=>$v){

		 		if($v->product){
					$data[$k] = [
			 			'id' => $v->id,
			 			'start_rate'=> rand(1,5),
						'incr_rate' => rand(50,200),
						'end_rate' => rand(85,99),
			 		];
		 		}
		 	}

		 	$finalResult = array_merge($data);
		 	
		 	$update = SeckillProduct::updateBatch($finalResult);
		 	
		 }

	    SeckillProduct::initCache();

	    //app(RewardGoldPrize::class)();

	    //sleep(1);

	    //app(CountGoldChildren::class)();

	    //sleep(1);

	    //GoldRound::initCache();
    }
}
